<?php
/**
 * Created by PhpStorm.
 * User: zengyuming
 * Date: 2017/8/9
 * Time: 上午10:07
 */

namespace Admin\Controller;
class PayChannelController extends CommonController{
    private static function mchdataLoad($pay_type){
        return M("mch_data")->field('mch_id,pay_type,mch_subject')->where(array('pay_type' => $pay_type))->select();
    }

    public function passageway_add(){
        if(IS_POST){
            $this->_tongdaoModify();
            exit;
        }
        $Id = I("request.id");
//        $price = intval(I("request.price"));
        $this->assign('merchant_id' , M("user")->query("select a.id  , b.merchant_id, b.Userremarks , a.passageway_price , a.passageway_group from zym_user as b left join zym_payconfig as a on a.effective_merchant = b.merchant_id;"));
        $this->displayConf($Id);

        $Model = M("payinfo");
        $this->assign(
            array(
                'passageway_weixinGzh_pay' => $Model->where("pay_type='weixinGzh_pay'")->select(),
                'passageway_WeixinScancode' => $Model->where("pay_type='WeixinScancode'")->select(),
                'passageway_alipay_pay' => $Model->where("pay_type='alipay_pay'")->select(),
                'passageway_jd_pay' => $Model->where("pay_type='jd_pay'")->select(),
                'passageway_qq_pay' => $Model->where("pay_type='qq_pay'")->select(),
                'passageway_bank_pay' => $Model->where("pay_type='bank_pay'")->select(),
            ));
        $this->assign('current_Titlename' , '通道添加|管理');
        $this->display('passageway_add1');
    }

    public function passageway_del($primaryId , $price = 0.000){
//        if(M("payconfig")->where(array('effective_merchant' => $mch_id , 'passageway_price' => $price))->delete()) {
        if(M("payconfig")->delete($primaryId)) {
            $this->success("删除成功");
        }else{
            $this->error('删除失败');
        }
    }

    public function passageway_list(){
        $tongdao_conf = M("payconfig")->alias('a')->field('a.* , b.Userremarks')->join("zym_user as b on a.effective_merchant = b.merchant_id")->select();
        // 取出指定全部商户的通道，因为链表的时候指定全部商户没有商户号
        $tdNum = (int)M("payconfig")->where(array('effective_merchant' => 0))->count();
        if( !$tdNum >= 2 ) {
            $tongdaoALLMerchant = M("payconfig")->where(array('effective_merchant' => 0))->find();
            $tongdao_conf[] = $tongdaoALLMerchant; // 再把一维数组放到二维数组中去正常循环取出
        }else{
            $tongdaoALLMerchant = M("payconfig")->where(array('effective_merchant' => 0))->select();
            foreach ($tongdaoALLMerchant as $value3) $tongdao_conf[] = $value3;
        }
        foreach ($tongdao_conf as $key => $item) {
//            if('alipay_pay' ||  $key == 'weixingzh_pay' || $key == 'qq_pay' || $key == 'weixinscancode') json_decode($item,true);
            $item['alipay_pay'] = json_decode($item['alipay_pay'] , true);
            $item['jd_pay'] = json_decode($item['jd_pay'] , true);
            $item['weixingzh_pay'] = json_decode($item['weixingzh_pay'] , true);
            $item['qq_pay'] = json_decode($item['qq_pay'] , true);
            $item['weixinscancode'] = json_decode($item['weixinscancode'] , true);
            $item['bank_pay'] = json_decode($item['bank_pay'] , true);


            $item['alipay_pay']['mch_channel'] = $item['alipay_pay']['mch_channel'] == '' ? '无指定通道' : $item['alipay_pay']['mch_id'] . '-' . $item['alipay_pay']['mch_channel'];
            $item['jd_pay']['mch_channel'] = $item['jd_pay']['mch_channel'] == '' ? '无指定通道' : $item['jd_pay']['mch_id'] . '-' . $item['jd_pay']['mch_channel'];
            $item['weixingzh_pay']['mch_channel'] = $item['weixingzh_pay']['mch_channel'] == '' ? '无指定通道' : $item['weixingzh_pay']['mch_id'] . '-' . $item['weixingzh_pay']['mch_channel'];
            $item['qq_pay']['mch_channel'] = $item['qq_pay']['mch_channel'] == '' ? '无指定通道' : $item['qq_pay']['mch_id'] . '-' . $item['qq_pay']['mch_channel'];
            $item['weixinscancode']['mch_channel'] = $item['weixinscancode']['mch_channel'] == '' ? '无指定通道' : $item['weixinscancode']['mch_id'] . '-' . $item['weixinscancode']['mch_channel'];
            $item['bank_pay']['mch_channel'] = $item['bank_pay']['mch_channel'] == '' ? '无指定通道' : $item['bank_pay']['mch_id'] . '-' . $item['bank_pay']['mch_channel'];

            $newarr[$key] = $item;
        }
        $this->assign('current_Titlename' , '通道列表');
        $this->assign('passageway_conf' , $newarr);
        $this->display();
    }

    /*  显示商户号指定所用的通道 */
    private function displayConf(){
        $Id = intval($_GET['id']);
        $where = array('id' => $Id, 'effective_merchant' => I('get.mch_id'));

        if($Id == 0 && empty($_GET['mch_id'])){ // 默认通道不需要指定商户号
            $where['effective_merchant'] = 0;
            unset($where['id']);
        }

        $mchconf = M("payconfig")->where($where)->find();
        $passageway_price = $mchconf['passageway_price'] != 0 ? $mchconf['passageway_price'] : 0.000;
        $this->assign('passageway_price' , $passageway_price); // 通道有效支付金额
        $this->assign('passageway_group' , $mchconf['passageway_group']); // 通道所属组
        $this->assign('continue_merchant' , $mchconf['continue_merchant']); // 在未指定支付通道时是否寻找默认通道


        $mchconf_alipay_pay = json_decode($mchconf['alipay_pay'] , true);
        $mchconf_jd_pay = json_decode($mchconf['jd_pay'] , true);
        $mchconf_weixinGzh_pay = json_decode($mchconf['weixingzh_pay'] , true); // weixingzh_pay字段写错了 ， 应该是weixinGzh_pay  编码问题吧
        $mchconf_qq_pay = json_decode($mchconf['qq_pay'] , true);
        $mchconf_WeixinScancode = json_decode($mchconf['weixinscancode'] , true);
        $mchconf_bank_pay = json_decode($mchconf['bank_pay'] , true);
        $this->assign(
            array(
                'mchconf_alipay_pay' => PayChannelController::mchdataLoad($mchconf_alipay_pay['payName']),
                'mchconf_jd_pay' => PayChannelController::mchdataLoad($mchconf_jd_pay['payName']),
                'mchconf_weixinGzh_pay' => PayChannelController::mchdataLoad($mchconf_weixinGzh_pay['payName']),
                'mchconf_qq_pay' => PayChannelController::mchdataLoad($mchconf_qq_pay['payName']),
                'mchconf_WeixinScancode' => PayChannelController::mchdataLoad($mchconf_WeixinScancode['payName']),
                'mchconf_bank_pay' => PayChannelController::mchdataLoad($mchconf_bank_pay['payName']),
            )
        );
        $this->assign(
            array(
                'alipay_pay_conf' => $mchconf_alipay_pay,
                'jd_pay_conf' => $mchconf_jd_pay,
                'weixinGzh_conf' => $mchconf_weixinGzh_pay,
                'bank_pay_conf' => $mchconf_bank_pay,
                'qq_pay_conf' => $mchconf_qq_pay,
                'WeixinScancode_conf' => $mchconf_WeixinScancode,
            )
        );

    }

    /* 通过支付方法名取商户表的PayName */
    private static function getPayName($MetHodName){
        $getPayName = M("payinfo")->where(array('Pay_MethodName' => $MetHodName))->getField('PayName');
        return $getPayName;
    }

    public function _tongdaoModify(){
        $data = I('post.' , '' , 'strip_tags,htmlspecialchars');
        $id = I("get.id" , '' , 'intval');
        $mch_id = I('get.mch_id' , '' , 'intval');
        $passageway_group = I('post.passageway_group' , '' , 'intval'); // 通道所属分组
        $datas['passageway_group'] = $passageway_group;
        $price = I('post.passageway_price' , '' , 'intval'); // 指定金额放通道
        $datas['passageway_price'] = ($price > 0) ? $price : 0;
        $datas['continue_merchant'] = I('post.continue_merchant' , '' , 'intval');
        $empty_merchant = intval($_POST['empty_merchant']);
        if($empty_merchant == 0 && $data['qq_pay'] == '' && $data['alipay_pay'] == '' && $data['weixinGzh_pay'] == '' && $data['WeixinScancode'] == '' && $data['jd_pay'] == '' && $data['bank_pay'] == ''){
            $this->error('必须选择一个指定通道');
            exit;
        }

        $datas['qq_pay'] =  PayChannelController::jsonstr($data['qq_pay'] , PayChannelController::getPayName($data['qq_pay']) , $data['qq_pay_mchId']);
        $datas['alipay_pay'] =  PayChannelController::jsonstr($data['alipay_pay'] , PayChannelController::getPayName($data['alipay_pay']) , $data['alipay_pay_mchId']);
        $datas['weixinGzh_pay'] =  PayChannelController::jsonstr($data['weixinGzh_pay'] , PayChannelController::getPayName($data['weixinGzh_pay']) , $data['weixinGzh_pay_mchId']);
        $datas['WeixinScancode'] =  PayChannelController::jsonstr($data['WeixinScancode'] , PayChannelController::getPayName($data['WeixinScancode']) , $data['WeixinScancode_mchId']);
        $datas['jd_pay'] =  PayChannelController::jsonstr($data['jd_pay'] , PayChannelController::getPayName($data['jd_pay']) , $data['jd_pay_mchId']);
        $datas['bank_pay'] =  PayChannelController::jsonstr($data['bank_pay'] , PayChannelController::getPayName($data['bank_pay']) , $data['bank_pay_mchId']);
        $datas['effective_merchant'] = isset($data['effective_merchant']) ? intval($data['effective_merchant']) : 0;
        $where = array( 'id' => $id , 'effective_merchant' => $mch_id ); // 商户号+数据库主键id来确定这条被修改的数据的准确性
        $C = M("payconfig")->where($where)->find();
        $is_add = I("post.is_add");
        if($C && $is_add != "1") {
            @M("payconfig")->where($where)->save($datas);
            $this->success('修改通道成功' , U("PayChannel/passageway_add") . '?id=' . $id . '&mch_id=' . $mch_id);
            exit;
        }else{
            $insertid = M("payconfig")->add($datas);
            $this->success('新增指定通道成功' ,  U("PayChannel/passageway_add") . '?id=' . $insertid .'&mch_id=' . $datas['effective_merchant'] );
            exit;
        }
    }

    private static function jsonstr($methodName , $channelName , $mch_id){
        return json_encode(array("mch_channel" => $methodName , 'payName' => $channelName , 'mch_id' => $mch_id));
    }

    public function passagewayMchidLoad(){
//        $Payname = C("payInfo");
        $pay_type = I("post.pay_type");
        $data = PayChannelController::mchdataLoad(PayChannelController::getPayName($pay_type));
        $this->ajaxReturn($data);
    }

}